package com.cqc.zhbjcqc.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.cqc.zhbjcqc.R;
import com.cqc.zhbjcqc.activity.MainActivity;
import com.cqc.zhbjcqc.bean.Frag2Bean;
import com.cqc.zhbjcqc.fragment.frag2.FragInteract;
import com.cqc.zhbjcqc.fragment.frag2.FragNews;
import com.cqc.zhbjcqc.fragment.frag2.FragPics;
import com.cqc.zhbjcqc.fragment.frag2.FragSubjects;
import com.cqc.zhbjcqc.net.MainRequest;
import com.cqc.zhbjcqc.net.NetUrl;
import com.cqc.zhbjcqc.utils.GsonUtil;
import com.cqc.zhbjcqc.utils.LogUtil;
import com.cqc.zhbjcqc.utils.SharedPreferencesUtil;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ${cqc} on 2016/9/27.
 * 新闻中心
 */

public class Frag2 extends BaseFragment {
    private static final String TAG = "Frag2";
    private List<String> titleList = new ArrayList<>();
    private ImageView iv_menu_top;
    private TextView tv_top;
    private ImageButton imgBtnTop;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frag2, container, false);
        findViews(view);
        initViews();
        return view;
    }

    private void initViews() {
        iv_menu_top.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                menu.toggle();
            }
        });
        imgBtnTop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager fm = ((MainActivity) context).getSupportFragmentManager();
                FragPics fragPics = (FragPics) fm.findFragmentByTag("FragPics");
                fragPics.switchListViewOrGridView(imgBtnTop);
            }
        });
    }

    private void findViews(View view) {
        iv_menu_top = (ImageView) view.findViewById(R.id.iv_menu_top);
        tv_top = (TextView) view.findViewById(R.id.tv_top);
        imgBtnTop = (ImageButton) view.findViewById(R.id.imgBtn_top);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        String json = SharedPreferencesUtil.getString(context, NetUrl.NEWS_CENTER_CATEGORIES, "");
        if (!TextUtils.isEmpty(json)) {
            parseJson(json);
        }

        MainRequest.getInstance().makePostRequest(NetUrl.NEWS_CENTER_CATEGORIES, null, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                LogUtil.d(TAG, "responese=" + responseInfo.result);
                SharedPreferencesUtil.saveString(context, NetUrl.NEWS_CENTER_CATEGORIES, responseInfo.result);
                parseJson(responseInfo.result);
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                LogUtil.d(TAG, "error=" + error.toString());

            }
        });
    }

    private void parseJson(String json) {
        Frag2Bean frag2Bean = GsonUtil.jsonToBean(json, Frag2Bean.class);
        titleList.clear();
        for (int i = 0; i < frag2Bean.getData().size(); i++) {
            String title = frag2Bean.getData().get(i).getTitle();
            titleList.add(title);
        }
        ((MainActivity) context).getFragMenuLeft().setData(frag2Bean.getData());
    }

    public void setTitle(int position){
        tv_top.setText(""+titleList.get(position));
        //如果是组图，则icon显示
        if (position == 2){
            imgBtnTop.setVisibility(View.VISIBLE);
        }
    }
}
